home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 3906 < prev    next >
Encoding:
Text File  |  1996-08-05  |  2.0 KB  |  53 lines

  1. Path: newshost.lanl.gov!tanmoy
  2. From: tanmoy@qcd.lanl.gov (Tanmoy Bhattacharya)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Unable to check calloc's return values
  5. Date: 31 Jan 1996 21:27:30 GMT
  6. Organization: Los Alamos National Laboratory
  7. Message-ID: <TANMOY.96Jan31142730@qcd.lanl.gov>
  8. References: <4emept$ifv@hpg30a.csc.cuhk.hk> <9601311141.AA03713@dxmint.cern.ch>
  9. NNTP-Posting-Host: qcd.lanl.gov
  10. Mime-Version: 1.0
  11. Content-Type: text
  12. In-reply-to: Dan Pop's message of Wed, 31 Jan 1996 12:41:50 +0100
  13.  
  14. In article <9601311141.AA03713@dxmint.cern.ch> Dan Pop
  15. <danpop@mail.cern.ch> writes: 
  16. <snip>
  17.    >Could anyone tell me why the following line is complained by the
  18.    >compiler? 
  19.    >
  20.    >if ((float **x = (float **) calloc(9, sizeof(float *)))
  21.      ^^^^^^^^
  22.    Because this is a syntax error in C.
  23.  
  24.    >   == 0) exit(1);
  25.    >
  26.    >What's the right way to check the calloc's return value?
  27.  
  28.        #include <stdlib.h>
  29.        float **x;
  30.        if ((x = calloc(9, sizeof(float *))) == NULL) exit(EXIT_FAILURE);
  31.        /* or, using your style:
  32.        if ((x = (float **) calloc(9, sizeof(float *))) == 0) exit(1);
  33.        */
  34.  
  35.    Note that the effect of exit(1) is implementation defined.
  36.    There are implementations where it means successful termination.
  37.  
  38. Also note that using calloc to allocate a bunch of pointers in
  39. portable code is a possible waste of time. (It does _not_ portably
  40. initialize them to NULL). Use malloc unless you want to allocate an
  41. `array' of some integral type (i.e. not pointers, not floating point
  42. types) initialized to 0: only in that case, does calloc make sense. 
  43.  
  44. Cheers
  45. Tanmoy
  46. --
  47. tanmoy@qcd.lanl.gov(128.165.23.46) DECNET: BETA::"tanmoy@lanl.gov"(1.218=1242)
  48. Tanmoy Bhattacharya O:T-8(MS B285)LANL,NM87545 H:#9,3000,Trinity Drive,NM87544
  49. Others see <gopher://yaleinfo.yale.edu:7700/00/Internet-People/internet-mail>,
  50. <http://alpha.acast.nova.edu/cgi-bin/inmgq.pl>or<ftp://csd4.csd.uwm.edu/pub/
  51. internetwork-mail-guide>. -- <http://nqcd.lanl.gov/people/tanmoy/tanmoy.html>
  52. fax: 1 (505) 665 3003   voice: 1 (505) 665 4733    [ Home: 1 (505) 662 5596 ]
  53.